package grabber;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * User: Vladimir
 * Date: 24.09.12 Time: 9:20
 */
public class ModelsChecker {
    public static void main(String[] args) throws IOException, InterruptedException {
        String sotmarketUrl = "http://www.sotmarket.ru/category/mobiles/brand/%s/pagenum-%s.html";
        String brand = "htc";
        int page = 1;
        List<String> models = new ArrayList<String>();
        while (true) {
            Document doc = null;
            try {
                doc = Jsoup.connect(String.format(sotmarketUrl, brand, page)).timeout(10000).get();
            } catch (IOException e) {
                break;
            }
            Elements goods = doc.select("div.b-catalog_goods-link");
            for (Element good : goods) {
                String modelName = good.child(0).text();
                modelName = filterModelName(modelName);
                models.add(modelName);
//                System.out.println(modelName);
            }
            page++;
            Thread.sleep(100);
        }

        List<String> absentModels = checkExtramobi("Аккумулятор", models);
//        for (String model : absentModels) {
//            System.out.println(model);
//        }
    }

    private static List<String> checkExtramobi(String prefix, List<String> models) throws IOException, InterruptedException {
        String extramobiSearchUrl = "http://extramobi.ru/search/?searchstring=%s";
        List<String> absentModels = new ArrayList<String>();
        for (String model : models) {
            String url = String.format(extramobiSearchUrl, prefix + "+" + model.replaceAll("\\s", "+"));
            Document doc = Jsoup.connect(url).timeout(10000).get();
            Elements products = doc.select("div.prdbrief_name");
            if (products.size() == 0) {
                absentModels.add(url);
                System.out.println(url);
            }
            Thread.sleep(200);
        }
        return absentModels;
    }

    private static String filterModelName(String modelName) {
        modelName = modelName.replace("2GB ", "");
        modelName = modelName.replace("4GB ", "");
        modelName = modelName.replace("8GB ", "");
        modelName = modelName.replace("16GB ", "");
        modelName = modelName.replace("32GB ", "");
        modelName = modelName.replace(" 2GB", "");
        modelName = modelName.replace(" 4GB", "");
        modelName = modelName.replace(" 8GB", "");
        modelName = modelName.replace(" 16GB", "");
        modelName = modelName.replace(" 32GB", "");
        modelName = modelName.replace(" 32GB", "");
        modelName = modelName.replace("Pink", "");
        modelName = modelName.replace("White", "");
        modelName = modelName.replace("Red", "");
        modelName = modelName.replace("Pebble Blue", "");
        modelName = modelName.replace("Marble White", "");
        modelName = modelName.replace("black", "");
        modelName = modelName.replace("SGH-", "");
        modelName = modelName.replace("GT-", "");
        modelName = modelName.trim();
        return modelName;
    }
}
